﻿#include "parameterrecond.h"
#include <QDebug>
#include"dbutil.h"
using namespace std;
ParameterRecord::ParameterRecord(QAbstractTableModel *parent) : QAbstractTableModel(parent)
{
}

int ParameterRecord::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return model_data.size();
}

int ParameterRecord::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return 3;
}

void ParameterRecord::setStartTime(QString s_time){
    StartTime=s_time.toStdString();
}

void ParameterRecord::setEndTime(QString e_time){
    EndTime=e_time.toStdString();
}

void ParameterRecord::getData(void){
    model_data.clear();
    Dbutil db;
    QSqlDatabase data= db.connect();
    QSqlQuery s_id;
    QSqlQuery e_id;
    QSqlQuery dataset;
    QSqlQuery recond_count;
    string sql_s_id;
    string sql_e_id;
    string sql_data;
    string recondcount;
    sql_s_id="select id from parameter_modfiy where time = '"+StartTime+"' limit 1";
    sql_e_id="select id from parameter_modfiy where time = '"+EndTime+"' limit 1";
    recondcount="select count(time) from parameter_modfiy where time = '"+EndTime+"'";
    const char *k_s_id = sql_s_id.c_str();
    const char *k_e_id = sql_e_id.c_str();
    const char *k_recond = recondcount.c_str();
    int start_id;
    int end_id;
    int datacount;
    bool success_s_id=s_id.exec(k_s_id);
    bool success_e_id=e_id.exec(k_e_id);
    bool success_count=recond_count.exec(k_recond);
    if(success_s_id&&success_e_id&&success_count){
        while(s_id.next()) start_id=s_id.value(0).toInt();
        while(e_id.next()) end_id=e_id.value(0).toInt();
        while(recond_count.next()) datacount=recond_count.value(0).toInt();
        end_id=end_id+datacount-1;
    }
    sql_data="select * from parameter_modfiy where id between "+to_string(start_id)+" and "+to_string(end_id);
    const char *k_data = sql_data.c_str();
    bool success_data=dataset.exec(k_data);
    if(success_data){
        int count=1;
        while(dataset.next()){
            model_data<<SimpleData{count,
                        dataset.value(2).toString(),
                        dataset.value(3).toString(),
                        dataset.value(4).toDouble(),dataset.value(5).toDouble(),
                        dataset.value(6).toDouble(),dataset.value(7).toDouble(),
                        dataset.value(8).toDouble(),dataset.value(9).toDouble(),
                        dataset.value(10).toDouble(),dataset.value(11).toDouble(),
                        dataset.value(12).toDouble(),dataset.value(13).toDouble()};
            count++;
        }
    }
    db.dbclose(data);
}

void ParameterRecord::update(void){
    beginResetModel();
    getData();
    endResetModel();
}

QVariant ParameterRecord::data(const QModelIndex &index, int role) const
{

    if(role == Qt::DisplayRole)
    {
        return QVariant();
    }
    if(index.column() < 0 ||
            columnCount() <= index.column() ||
            index.row() < 0 ||
            rowCount() <= index.row())
    {
        return QVariant();
    }
    switch(role)
    {
    case IdRole:
        return model_data[index.row()].m_id;
    case TimeRole:
        return  model_data[index.row()].m_time;
    case OperatorRole:
        return  model_data[index.row()].m_operator;
    case SO2MinRole:
        return  model_data[index.row()].m_so2min;
    case SO2MaxRole:
        return  model_data[index.row()].m_so2max;
    case NOXMinRole:
        return  model_data[index.row()].m_noxmin;
    case NOXMaxRole:
        return  model_data[index.row()].m_noxmax;
    case ParticulatesMinRole:
        return  model_data[index.row()].m_particulatesmin;
    case ParticulatesMaxRole:
        return  model_data[index.row()].m_particulatesmax;
    case O2MinRole:
        return  model_data[index.row()].m_o2min;
    case O2MaxRole:
        return  model_data[index.row()].m_o2max;
    case COMinRole:
        return  model_data[index.row()].m_comin;
    case COMaxRole:
        return  model_data[index.row()].m_comax;
    default:
        qDebug() << "Not supposed to happen";
        return QVariant();
    }
}

QHash<int, QByteArray> ParameterRecord::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[IdRole] = "xuhao";
    roles[TimeRole] = "shijian";
    roles[OperatorRole] = "caozuoyuan";
    roles[SO2MinRole] = "so2dixian";
    roles[SO2MaxRole] = "so2gaoxian";
    roles[NOXMinRole] = "noxdixian";
    roles[NOXMaxRole] = "noxgaoxian";
    roles[ParticulatesMinRole] = "kelidixian";
    roles[ParticulatesMaxRole] = "keligaoxian";
    roles[O2MinRole] = "o2dixian";
    roles[O2MaxRole] = "o2gaoxian";
    roles[COMinRole] = "codixian";
    roles[COMaxRole] = "cogaoxian";
    return roles;
}
